package com.sskj.wallet.ui.fragment;

import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.util.CommonUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.bus.WalletAssetBean;
import com.sskj.wallet.presenter.WalletAssetAllPresenter;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_FRAGMENT_ASSET_ALL)
public class WalletAssetAllFragment extends BaseFragment<WalletAssetAllPresenter> {


    @BindView(R2.id.iv_see)
    ImageView ivSee;
    @BindView(R2.id.tv_wallet_usdt)
    TextView tvWalletUsdt;
    @BindView(R2.id.tv_wallet_cny)
    TextView tvWalletCny;
    @BindView(R2.id.tv_recharge)
    TextView tvRecharge;
    @BindView(R2.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R2.id.tv_transfer)
    TextView tvTransfer;

    private String cny = "0.00";
    private String usdt = "0.0000";
    private int isSee;
    private String canSee;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_fragment_wallet_assetall;
    }

    @Override
    public WalletAssetAllPresenter getPresenter() {
        return new WalletAssetAllPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        canSee = SPUtil.get(SPConfig.ALL_EYE, "1");
        //充币
        ClickUtil.click(300, tvRecharge, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECHARGE).navigation();
            }
        });
        //提币
        ClickUtil.click(300, tvWithdraw, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_WITHDRAW).navigation();
            }
        });
        //划转
        ClickUtil.click(300, tvTransfer, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_TRANSFER)
                        .withString(Constans.WALLET_TYPE, Constans.WALLET_ALL).navigation();
            }
        });
        ClickUtil.click(300, ivSee, () -> {
            if (isSee == 0) {
                SPUtil.put(SPConfig.ALL_EYE, "0");
                RxBus.getDefault().send(RxBusCode.CLOSE_EYE);
                tvWalletUsdt.setText("******");
                tvWalletCny.setText("******");
                ivSee.setImageResource(R.mipmap.lib_unsee);
                isSee = 1;
            } else {
                SPUtil.put(SPConfig.ALL_EYE, "1");
                RxBus.getDefault().send(RxBusCode.OPEN_EYE);
                tvWalletUsdt.setText(usdt);
                tvWalletCny.setText("≈ " + cny + " CNY");
                ivSee.setImageResource(R.mipmap.lib_see);
                isSee = 0;
            }
        });
    }

    @Override
    public void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updataAsset(WalletAssetBean walletAssetBean) {
        if (walletAssetBean != null && walletAssetBean.getWalletType().equals(Constans.WALLET_ALL)) {
            ivSee.setImageResource("1".equals(canSee) ? R.mipmap.lib_see : R.mipmap.lib_unsee);

            usdt = CommonUtil.dealCoinNum(walletAssetBean.getToto_price(), Constans.COIN_USDT);
            cny = NumberUtil.keep2(walletAssetBean.getToto_cny_pric());
            if ("1".equals(SPUtil.get(SPConfig.ALL_EYE, "1"))) {
                tvWalletUsdt.setText(CommonUtil.dealCoinNum(walletAssetBean.getToto_price(), Constans.COIN_USDT));
                tvWalletCny.setText("≈ " + NumberUtil.keep2(walletAssetBean.getToto_cny_pric()) + " CNY");
            } else {
                tvWalletUsdt.setText("******");
                tvWalletCny.setText("******");
            }
        }
    }

}
